package com.common.db;

import java.util.List;   

public class Pg {   

	public final static int PAGESIZE = 10;   

	private int pageSize = PAGESIZE;   

	private List items;
	
	private Object obj;

	private int totalCount;   

	private int[] indexes = new int[0];   

	private int startIndex = 0;  

	private int firstIndex = 0;  

	private int lastIndex = 0;  

	private int previousIndex = 0;

	private int nextIndex = 0;

	private int currentPageNo = 0;

	private int totalPageNo =0;

	public Pg(List items, int totalCount) {   
		setPageSize(PAGESIZE);   
		setTotalCount(totalCount);   
		setItems(items);           
		setStartIndex(0); 
		setNextIndex();
		setPreviousIndex();
		setCurrentPageNo();
		setTotalPageNo();
		setFirstIndex();
		setLastIndex();
	}   

	public Pg(List items, int totalCount, int startIndex) {   
		setPageSize(PAGESIZE);   
		setTotalCount(totalCount);   
		setItems(items);           
		setStartIndex(startIndex); 
		setNextIndex();
		setPreviousIndex();
		setCurrentPageNo();
		setTotalPageNo();
		setFirstIndex();
		setLastIndex();
	}   

	public Pg(List items, int totalCount, int pageSize, int startIndex) {   
		setPageSize(pageSize);   
		setTotalCount(totalCount);   
		setItems(items);   
		setStartIndex(startIndex);  
		setNextIndex();
		setPreviousIndex();
		//setCurrentPageNo(startIndex,pageSize); //����д�Ͳ����javaBean�淶��jstl�������Ի����
		//setTotalPageNo(totalCount, pageSize);  //ͬ��
		setCurrentPageNo();
		setTotalPageNo();
		setFirstIndex();
		setLastIndex();
	}   

	public List getItems() {   
		return items;   
	}   

	public void setItems(List items) {   
		this.items = items;   
	}   

	public int getPageSize() {   
		return pageSize;   
	}   

	public void setPageSize(int pageSize) {   
		this.pageSize = pageSize;   
	}   

	public int getTotalCount() {   
		return totalCount;   
	}   

	public void setTotalCount(int totalCount) {   
		if (totalCount > 0) {   
			this.totalCount = totalCount;   
			int count = totalCount / pageSize;   
			if (totalCount % pageSize > 0)   
				count++;   
			indexes = new int[count];   
			for (int i = 0; i < count; i++) {   
				indexes[i] = pageSize * i;   
			}   
		} else {   
			this.totalCount = 0;   
		}   
	}   

	public int[] getIndexes() {   
		return indexes;   
	}   

	public void setIndexes(int[] indexes) {   
		this.indexes = indexes;   
	}   

	public int getStartIndex() {   
		return startIndex;   
	}   

	public void setStartIndex(int startIndex) {   
		if (totalCount <= 0)   
			this.startIndex = 0;   
		else if (startIndex >= totalCount)   
			this.startIndex = indexes[indexes.length - 1];   
		else if (startIndex < 0)   
			this.startIndex = 0;   
		else {   
			this.startIndex = indexes[startIndex / pageSize];   
		}   
	}   

	public int getNextIndex() {   
		return nextIndex;   
	}   

	public void setNextIndex() {   
		nextIndex = getStartIndex() + pageSize;   
		if (nextIndex >= totalCount)   
			nextIndex = getStartIndex();   
	} 

	public int getFirstIndex() {   
		return firstIndex;   
	}   

	public void setFirstIndex() {   
		firstIndex = 0;   
	} 

	public int getLastIndex() {   
		return lastIndex;   
	}   

	public void setLastIndex() {     
		if (getTotalCount()%pageSize==0)   
			lastIndex = (getTotalCount()/pageSize - 1)*pageSize;
		else
			lastIndex = (getTotalCount()/pageSize)*pageSize; 
	} 

	public int getPreviousIndex() {   
		return previousIndex;   
	}   

	public void setPreviousIndex() {   
		previousIndex = getStartIndex() - pageSize;   
		if (previousIndex < 0)   
			previousIndex = 0;   
	}  

	public void setCurrentPageNo(){
		if(getStartIndex()%pageSize!=0)
			currentPageNo = getStartIndex()/pageSize +2;
		else 
			currentPageNo = getStartIndex()/pageSize + 1 ;
	}

	public int getCurrentPageNo(){
		return	currentPageNo ;
	}

	public void setTotalPageNo(){
		if(getTotalCount()%pageSize!=0)
			totalPageNo = getTotalCount()/pageSize +1;
		else 
			totalPageNo = getTotalCount()/pageSize ;
	}

	public int getTotalPageNo(){
		return	totalPageNo ;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}
	
}  
